import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {BannerListDetail, detailList, getarticle, getBannerList,getSiderList, getTag} from '../../../services'
export interface IndexModelState {
  name?: string;
  list:[],
  bannerlist?:[],
  siderList:[],
  tagList:[],
  detail:[],
  detailList:[],

}
export interface IndexModelType {
  namespace: 'articles1';
  state: IndexModelState;
  effects: {
    getarticle: Effect;
    bannerListDetail:Effect;
    detailList:Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
}

const articleModel: IndexModelType = {
  namespace: 'articles1',

  state: {
    name: 'article',
    list:[],
    bannerlist:[],
    siderList:[],
    tagList:[],
    detail:[],
    detailList:[],
  },

  effects: {
    *getarticle({ payload }, { call, put }) {
      let result =yield call(getarticle,{page:1,pageSize:12})    
      let result1 =yield call(getBannerList)    
      let siderList=yield call(getSiderList)
      let TagList=yield call(getTag)
      yield put({
        type:'save',
        payload:{
          list:result,
          bannerlist:result1,
          siderList:siderList,
          tagList:TagList
        }
      })
    },
    *bannerListDetail({payload} ,{call,put}){
      let result =yield call(BannerListDetail,payload.id)
      let detaillist=yield call(detailList,payload.id)
      console.log(result);
      yield put({
        type:'save',
        payload:{
          detail:result,
          detailList:detaillist.data.splice(0,6)
        }
      })
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default articleModel;